//
//  ReadDetailViewController.m
//  WYnewsss
//
//  Created by lanou on 16/4/21.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadDetailViewController.h"
#import "NetWorkrequestManager.h"
#import "NSString+Html.h"
@interface ReadDetailViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *contentWebView;
@property(nonatomic,strong)NSString *contentString;
@end

@implementation ReadDetailViewController

-(void)requestData{
    NSString *urlStr = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html",_docid];
    [NetWorkRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",dataDic);
        _contentString =dataDic[self.docid][@"body"];
        
        NSArray *array =dataDic[self.docid][@"img"];
        for (NSDictionary *dic in array) {
            NSLog(@"%@",dic);
            NSLog(@"%@",dic[@"ref"]);
            NSString *string =[_contentString stringByReplacingOccurrencesOfString:dic[@"ref"] withString:dic[@"src"]];
            
            
            
            _contentWebView.scalesPageToFit = NO;
            NSString *newString =[NSString importStyleWithHtmlString:string];
            NSURL *url =[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
            [_contentWebView loadHTMLString:newString baseURL:url];
            
        }
  
    } error:^(NSError *error) {
        
    }];
}
    
//    [NetWorkrequestManage requestWithType:GET url:urlStr parameters:nil finish:^(NSData *data) {
//        NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        //NSLog(@"%@",dataDic);
//        _contentString =dataDic[self.docid][@"body"];
//        
//        NSArray *array =dataDic[self.docid][@"img"];
//        for (NSDictionary *dic in array) {
//            NSLog(@"%@",dic);
//            NSLog(@"%@",dic[@"ref"]);
//            NSString *string =[_contentString stringByReplacingOccurrencesOfString:dic[@"ref"] withString:dic[@"src"]];
//          
//    
//       
//            _contentWebView.scalesPageToFit = NO;
//            NSString *newString =[NSString importStyleWithHtmlString:string];
//            NSURL *url =[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
//            [_contentWebView loadHTMLString:newString baseURL:url];
//
//        }
//        
//               // NSLog(@"%@",_contentString);
//    } error:^(NSError *error) {
//        
//    }];




- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
